using SS;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SM.core
{
	internal class Order
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public Order()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(string ID, string Name, string Address, string Phone, string Company, string Acc)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabDoctor", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Name", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Address", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Phone", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Company", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("Acc", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["Name"].Value = Name;
				sqlCommand.Parameters["Address"].Value = Address;
				sqlCommand.Parameters["Phone"].Value = Phone;
				sqlCommand.Parameters["Company"].Value = Company;
				sqlCommand.Parameters["Acc"].Value = Acc;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public void loadGridView(DataGridView dgv, BindingNavigator bdn)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string selectCommandText = "SELECT ID, Name, Address, Phone, Company, Acc FROM [TabDoctor] ORDER BY [Name]";
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, sqlConnection);
			DataSet dataSet = new DataSet();
			sqlDataAdapter.Fill(dataSet);
			BindingSource bindingSource = new BindingSource();
			bindingSource.DataSource = dataSet.Tables[0];
			bdn.BindingSource = bindingSource;
			dgv.DataSource = bindingSource;
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public string getDoctorName(string Acc, ref string doctorName)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT ID, Name FROM [TabDoctor] WHERE [Acc] = '" + Acc + "'";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = sqlDataReader["ID"].ToString();
				doctorName = sqlDataReader["Name"].ToString();
			}
			sqlDataReader.Close();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public void saveOrder(string ID, DateTime CreateDate, string Note, string PID, string UserName, string FullName)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabOrder", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("CreateDate", SqlDbType.DateTime);
				sqlCommand.Parameters.Add("Note", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("PID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("UserName", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("FullName", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["CreateDate"].Value = CreateDate;
				sqlCommand.Parameters["Note"].Value = Note;
				sqlCommand.Parameters["PID"].Value = PID;
				sqlCommand.Parameters["UserName"].Value = UserName;
				sqlCommand.Parameters["FullName"].Value = FullName;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public int saveOrderDetail(string billID, DataTable objTable)
		{
			int result;
			try
			{
				if (billID == "-:-")
				{
					billID = this.getNewOrderBillID();
				}
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabOrderDetail", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("billID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Pid", SqlDbType.Int);
				sqlCommand.Parameters.Add("ProductName", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("ProductUnit", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("ProductNumber", SqlDbType.Float);
				sqlCommand.Parameters.Add("NationalID", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters["billID"].Value = billID;
				for (int i = 0; i < objTable.Rows.Count; i++)
				{
					sqlCommand.Parameters["Pid"].Value = objTable.Rows[i]["ProductID"].ToString();
					sqlCommand.Parameters["ProductName"].Value = objTable.Rows[i]["ProductName"].ToString();
					sqlCommand.Parameters["ProductUnit"].Value = objTable.Rows[i]["ProductUnit"].ToString();
					sqlCommand.Parameters["ProductNumber"].Value = objTable.Rows[i]["ProductNumber"].ToString();
					sqlCommand.Parameters["NationalID"].Value = objTable.Rows[i]["NationalID"].ToString();
					sqlCommand.ExecuteNonQuery();
				}
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public string getNewOrderBillID()
		{
			string result = "";
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("getNewOrderBillID", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Direction = ParameterDirection.Output;
				sqlCommand.Parameters["ID"].Value = "";
				sqlCommand.ExecuteNonQuery();
				result = sqlCommand.Parameters["ID"].Value.ToString();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
			return result;
		}
		public DataTable getOrderDetail(string billID)
		{
			DataTable result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT PID as ProductID,ProductName,ProductUnit,ProductNumber,NationalID FROM [TabOrderDetail] WHERE [billID] = '" + billID + "'", sqlConnection);
				DataSet dataSet = new DataSet();
				sqlDataAdapter.Fill(dataSet, "TabOrderDetail");
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = dataSet.Tables["TabOrderDetail"];
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				result = null;
			}
			return result;
		}
		public int delOrder(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_DellOrder", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public int delItem(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				string cmdText = "DELETE [TabDoctor] WHERE [ID] = '" + ID + "'";
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public string CheckEmpExitstInOutBill(string ID)
		{
			string result = "";
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			try
			{
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT [ID] FROM [TabBill] WHERE [_DoctorID] = '" + ID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					result = "1";
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
			}
			catch
			{
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string cusPrescription(string ID, string _Name, string _Age, string _Sex, string _Address, string _Phone)
		{
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("sp_TabPatients", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Direction = ParameterDirection.Output;
				sqlCommand.Parameters.Add("_Name", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("_Age", SqlDbType.Int);
				sqlCommand.Parameters.Add("_Sex", SqlDbType.Int);
				sqlCommand.Parameters.Add("_Address", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("_Phone", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["_Name"].Value = _Name;
				sqlCommand.Parameters["_Age"].Value = _Age;
				sqlCommand.Parameters["_Sex"].Value = _Sex;
				sqlCommand.Parameters["_Address"].Value = _Address;
				sqlCommand.Parameters["_Phone"].Value = _Phone;
				sqlCommand.ExecuteNonQuery();
				string text = sqlCommand.Parameters["ID"].Value.ToString();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = text;
			}
			catch
			{
				result = "";
			}
			return result;
		}
	}
}
